//
//  KZOptionPanelView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/18.
//  Copyright © 2018年 zhukai. All rights reserved.
//

#import "KZOptionPanelView.h"

#import "KZOptionView.h"


@implementation KZOptionPanelView
{
    UIControl *_selectedOptionView;
}

- (instancetype)init {
    if (self = [super init]) {
        _selectedOptionView = nil;
    }
    return self;
}

- (void)setupUI {
    
    NSArray *keys = [_options.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSString *key1, NSString *key2) {
        return [key1 compare:key2] == NSOrderedDescending;
    }];
    
    
    UIView *last = nil;
    for (int i = 0; i < keys.count; i ++) {
        
        UIControl *basalView = [UIControl new];
        basalView.tag = i;
        [basalView addTarget:self action:@selector(onTouchOption:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:basalView];
        [basalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(@0);
            if (last) {
                make.top.equalTo(last.mas_bottom);
            } else {
                make.top.mas_equalTo(0);
            }
            // 高度子视图决定
        }];
        last = basalView;

        NSString *key = keys[i];
        NSString *value = _options[key];
        
        KZOptionView *optionView = [KZOptionView new];
        optionView.tag = 888;
        optionView.text = key;
        [basalView addSubview:optionView];
        [optionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(W(15));
            make.width.height.mas_equalTo(W(40));
            make.top.mas_equalTo(0);
            // 父视图高度
            make.bottom.mas_equalTo(-H(20));
        }];
        optionView.status = KZOptionStatus_none;
        
        
        UILabel *contentLabel = [[UILabel alloc] init];
        contentLabel.numberOfLines = 0;
        contentLabel.text = value;
        contentLabel.font = FONT(H(16));
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.textColor = [UIColor blackColor];
        [basalView addSubview:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(optionView.mas_right).offset(W(15));
            // 文字的第一行的中心和选项(A)对齐
            make.top.equalTo(optionView.mas_centerY).offset(-H(8));
            make.right.mas_equalTo(-W(15));
        }];
    }
    // 设置父视图高度
    [last mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
    }];
}
- (void)setOptions:(NSDictionary *)options {
    _options = options;
    [self setupUI];
}

#pragma mark - 事件
- (void)onTouchOption:(UIControl *)sender {

    if (sender == _selectedOptionView) {
        return;
    }
    
    // 更换选中
    _selectedOptionView.selected = false;
    ((KZOptionView *)[_selectedOptionView viewWithTag:888]).status = KZOptionStatus_none;
    sender.selected = true;
    ((KZOptionView *)[sender viewWithTag:888]).status = KZOptionStatus_select;
    _selectedOptionView = sender;
    
}
@end
